package com.carkeeper.menu.dao;

import com.carkeeper.pojo.Menu;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;

import java.util.LinkedHashSet;
import java.util.List;

@Repository
public interface MenuMapper extends Mapper<Menu> {

    /**
     * 根据角色id查询角色对应的一级菜单
     * @param roleId
     * @return
     */
    @Select("select * from tb_menu where id in (select menu_id from tb_role_menu where role_id = #{roleId})")
    LinkedHashSet<Menu> findMenuListByRoleId(@Param("roleId") Integer roleId);

    /**
     * 根据父菜单id查询子菜单
     * @param menuId
     * @return
     */
    @Select("SELECT DISTINCT t2.* FROM tb_menu AS t1, tb_menu AS t2 where  t2.level = 2 AND t2.parent_menu_id = #{menuId}")
    List<Menu> findChildByParentRoleId(@Param("menuId") Integer menuId);

    /**
     * 根据角色id和菜单id新增/修改角色对应的菜单列表
     * @param roleId
     * @param menuId
     * @return
     */
    @Insert("insert into tb_role_menu values (#{roleId}, #{menuId})")
     int addMenuListByRoleIdAndMenuId(@Param("roleId") Integer roleId, @Param("menuId") Integer menuId);

    /**
     * 根据roleId删除中间表相关数据
     * @param roleId
     * @return
     */
    @Delete("delete from tb_role_menu where role_id = #{roleId}")
    int deleteMenuListByRoleId(@Param("roleId") Integer roleId);

    /**
     * 通过角色id查询角色菜单表的数据
     * @param roleId
     * @return
     */
    @Select("select * from tb_role_menu where role_id = #{roleId}")
    LinkedHashSet<Menu> findMenuList2ByRoleId(@Param("roleId") Integer roleId);
}
